//
//  SEGSharedNewManager.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/10/17.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGSharedNewManager.h"
#import <SEGPlatform/SEGAdDisplayManager.h>


@interface SEGSharedNewManager ()

/** 详情评论总页数 */
@property (nonatomic, assign) NSInteger commentsTotalSize;
/// 记录上次的广告下标
@property (nonatomic, assign) __block NSInteger adverIndex;

@end


@implementation SEGSharedNewManager
/**
 * 获取邻里头条
 * @author Wendy
 */
- (void)requestTopLine:(SEGRequestCallBack)finished
{
    NSString *cityID = [SEGProfileManager sharedManager].profile.cityId;
    NSDictionary *params = @{@"cityId":cityID};
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kCMS_api,@"/bbs/queryNeighborhood.json",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSArray* list = [dicData valueForKey:@"neighborhood"];
        
        if (!list || ![list isKindOfClass:[NSArray class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSMutableArray* arrM = [NSMutableArray arrayWithCapacity:list.count];
        for (NSDictionary* dic in list) {
            SEGSharedTopLineModel* _model = [SEGSharedTopLineModel initWithDictionary:dic];
            [arrM addObject:_model];
        }
        
        NSMutableArray* _tmpData = [NSMutableArray arrayWithArray:self.topLineArray];
        [_tmpData addObjectsFromArray:arrM];
        
        self.topLineArray = _tmpData;
        
        finished(YES, codeType, errorMessage);
        
    }];
}

/**
 * 获取侧滑推荐位
 * @author Wendy
 * @param pageNo  页码
 * @param pageLimit  每页数据条数
 */
- (void)requestRecommendWithPageNo:(NSString *)pageNo
                         pageLimit:(NSString *)pageLimit
                          finished:(SEGRequestCallBack)finished
{
    NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:pageNo, @"pageNo", pageLimit, @"pageLimit", nil];
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kCMS_api,@"/bbs/queryRecommendIndex.json",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        finished(isSuccessed, codeType, errorMessage);
        
    }];
}

/**
 * 获取侧滑推荐位--查看更多
 * @author Wendy
 * @param pageNo  页码
 * @param pageLimit  每页数据条数
 */
- (void)requestMoreRecommendWithPageNo:(NSString *)pageNo
                             pageLimit:(NSString *)pageLimit
                              finished:(SEGRequestCallBack)finished
{
    NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:pageNo, @"pageNo", pageLimit, @"pageLimit", nil];
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kCMS_api,@"/bbs/queryRecommend.json",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSArray* list = [dicData valueForKey:@"recommend"];
        
        if (!list || ![list isKindOfClass:[NSArray class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        if ([pageNo integerValue] != 1 && [list isKindOfClass:[NSArray class]] && list.count == 0) {
            finished(NO, codeType, @"没有更多的数据！");
            return;
        }
        
        if ([pageNo integerValue] == 1) { //顶部刷新
            self.recommendMoreArray = @[];
        }
        
        NSMutableArray* arrM = [NSMutableArray arrayWithCapacity:list.count];
        for (NSDictionary* dic in list) {
            SEGSharedRecommendModel* _model = [SEGSharedRecommendModel initWithDictionary:dic];
            [arrM addObject:_model];
        }
        NSMutableArray* _tmpData = [NSMutableArray arrayWithArray:self.recommendMoreArray];
        [_tmpData addObjectsFromArray:arrM];
        
        self.recommendMoreArray = _tmpData;
        
        finished(YES, codeType, errorMessage);
        
    }];
}

/**
 * 获取邻里圈列表某一项的所有子评论列表 新接口
 * @author Wendy
 * @param objId  主键id
 * @param objType  类型
 */
- (void)requestAllQueryCommentsWithObjId:(NSString *)objId
                                 objType:(NSString *)objType
                                finished:(SEGRequestCallBack)finished
{
    NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:objId, @"objId", objType, @"objType", nil];
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/api/v2/queryComments",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        self.childCommentsArray = [NSMutableArray array];
        NSArray* list = [dicData valueForKey:@"comments"];
        
        if (list && [list isKindOfClass:[NSArray class]]) {
            for (NSDictionary* dic in list) {
                SEGSharedCommentChildCommentModel* _model = [SEGSharedCommentChildCommentModel initWithDictionary:dic];
                [self.childCommentsArray addObject:_model];
            }
        }
        
        finished(YES, codeType, errorMessage);
        
    }];
}



/**
 * 删除参与记录
 * @author Wendy
 * @param objId
 * @param objType  类型
 */
- (void)requestDeleteJoinHistoryWithObjId:(NSString *)objId
                                  objType:(NSString *)objType
                                 finished:(SEGRequestCallBack)finished
{
    NSDictionary *params = @{@"objId":ValueToString(objId), @"objType": ValueToString(objType)};
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kCMS_api,@"/bbs/deleteJoinHistory",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if(isSuccessed) {
            // 发送通知更新列表
            [[NSNotificationCenter defaultCenter] postNotificationName:SEGDeletaFinishedNotification object:params];
            
            //请求积分行为接口
            [[SEGAnalyseBehaviorManager sharedManager] requestIntegralGrowthWithCode:SEGIntegralCode.fornumIsDeleted finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            }];
        }
        finished(isSuccessed, codeType, errorMessage);
        
    }];
}

/**
 * 删除评论
 * @author Wendy
 * @param commentId  评论id
 * @param objType  类型
 */
- (void)requestDeleteCommentWithCommentId:(NSString *)commentId
                                  objType:(NSString *)objType
                                 finished:(SEGRequestCallBack)finished
{
    NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys: commentId, @"objId", objType, @"objType", nil];
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kCMS_api,@"/bbs/deleteComment",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if(isSuccessed) {
            //请求积分行为接口
            
            [[SEGAnalyseBehaviorManager sharedManager] requestIntegralGrowthWithCode:SEGIntegralCode.replyIsDeleted finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            }];
        }
        finished(isSuccessed, codeType, errorMessage);
        
    }];
}

/**
 * 获取评论列表
 * @author Wendy
 * @param objId  主键id
 * @param objType  类型
 * @param pageNo  页码
 * @param pageLimit  每页数据条数
 */
- (void)requestQueryCommentsWithObjId:(NSString *)objId
                              objType:(NSString *)objType
                               pageNo:(NSString *)pageNo
                            pageLimit:(NSString *)pageLimit
                             finished:(SEGRequestCallBack)finished
{
    /**
     最后一页, 无需再次请求接口
     
     条件一: pageNum != 1, 防止下拉刷新时 return;
     条件二: pageNum > 总页数, 说明已经拿到所有数据, 无需再次调用接口 return;
     */
    if (([pageNo integerValue] != 1) && [pageNo integerValue] > _commentsTotalSize) {
        finished(NO, nil, kNoMoreDataContent);
        return;
    }
    
    NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:objId, @"objId", objType, @"objType", pageNo, @"pageNo", pageLimit, @"pageLimit", nil];
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/bbs/queryComments",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dicData = [result valueForKey:@"data"];
        self.commentsTotalSize = [dicData[@"totalPage"] integerValue]; // 总页数
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        // 评论总数
        if (dicData[@"count"]) self.commentAmount = dicData[@"count"];

        NSArray* list = [dicData valueForKey:@"comments"];
        
        if (!list || ![list isKindOfClass:[NSArray class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        if ([pageNo integerValue] == 1) { //顶部刷新
            self.commentsArray = [NSMutableArray array];
        }
        for (NSDictionary* dic in list) {
            SEGSharedCommentModel* _model = [SEGSharedCommentModel initWithDictionary:dic];
            [self.commentsArray addObject:_model];
        }
        
        finished(YES, codeType, errorMessage);
        
    }];
}

/**
 * 获取邻里圈列表
 * @author Wendy
 * @param scopeId  范围 1：本社区  2：周城汇  3：四合院
 * @param objId  邻里头条id 可选
 * @param cityId  当前登录人的市id
 * @param objType 类型 可选
 * @param pageNo  页码
 * @param pageLimit  每页数据条数
 * @param communityId 社区ID
 * @param moduleId 1：纷享页，2：邻里圈
 */
- (void)requestNeighborhoodWithScopeId:(NSString *)scopeId
                                 objId:(NSString *)objId
                                cityId:(NSString *)cityId
                               objType:(NSString *)objType
                                pageNo:(NSString *)pageNo
                             pageLimit:(NSString *)pageLimit
                           communityId:(NSString *)communityId
                              moduleId:(NSString *)moduleId
                              finished:(SEGRequestCallBack)finished
{
    /**
     最后一页, 无需再次请求接口
     
     条件一: pageNum != 1, 防止下拉刷新时 return;
     条件二: pageNum > 总页数, 说明已经拿到所有数据, 无需再次调用接口 return;
     */
    if (([pageNo integerValue] != 1) && [pageNo integerValue] > _neighborhoodTotalSize) {
        finished(NO, nil, kNoMoreDataContent);
        return;
    }
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:scopeId, @"scopeId", pageNo, @"pageNo", pageLimit, @"pageLimit", nil];
    if (objId != nil && objType != nil) {
        [params setObject:objId forKey:@"objId"];
        [params setObject:objType forKey:@"objType"];
    }
    NSString *urlString = nil;
    
    urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/api/v2/queryNeighborhoodMore",nil];
    if (communityId && communityId.length > 0) [params setObject:communityId forKey:@"communityId"];
    if (moduleId && moduleId.length > 0) [params setObject:moduleId forKey:@"moduleId"];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        if ([pageNo integerValue] == 1) { //顶部刷新
            self.adverIndex = 0; // 更新广告下标位
            self.neighborhoodListArray = [NSMutableArray array];
        }
        
        NSDictionary* dicData = [result valueForKey:@"data"];
        self.neighborhoodTotalSize = [dicData[@"totalPage"] integerValue]; // 总页数
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(YES, codeType, errorMessage);
            return;
        }
        
        NSArray* list = [dicData valueForKey:@"neighborhood"];
        if (!list || ![list isKindOfClass:[NSArray class]]) {
            finished(YES, codeType, errorMessage);
            return;
        }
        
        NSMutableArray<SEGSharedNeihborhoodModel *> *tempNeighborhoodList = [NSMutableArray array];
        
        for (NSDictionary* dic in list) {
            SEGSharedNeihborhoodModel* _model = [SEGSharedNeihborhoodModel initWithDictionary:dic];
            __block BOOL isNotLike = NO;
            //本地过滤不感兴趣的数据
            NSDictionary *tempDict = [[NSUserDefaults standardUserDefaults] objectForKey:kNeighborhoodNotLikeArray];
            if (tempDict && [tempDict isKindOfClass:[NSDictionary class]]) {
                __block NSArray *array;
                if ([tempDict objectForKey:kAccountUserID]) {
                    array = [tempDict objectForKey:kAccountUserID];
                }
                if (array && [array isKindOfClass:[NSArray class]]) {
                    [array enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL * _Nonnull stop) {
                        NSString *objType = ValueToString(dic[@"objType"]);
                        NSString *objId = ValueToString(dic[@"objId"]);
                        if([objType isEqualToString:_model.model.objType] && [objId isEqualToString:_model.model.objId]) {
                            isNotLike = YES;
                            *stop = YES;
                        }
                    }];
                }
            }
            __block BOOL isReported = NO;
            //本地过滤举报过的数据
            tempDict = [[NSUserDefaults standardUserDefaults] objectForKey:kNeighborhoodReportedArray];
            if (tempDict && [tempDict isKindOfClass:[NSDictionary class]]) {
                __block NSArray *array;
                if ([tempDict objectForKey:kAccountUserID]) {
                    array = [tempDict objectForKey:kAccountUserID];
                }
                if (array && [array isKindOfClass:[NSArray class]]) {
                    [array enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL * _Nonnull stop) {
                        NSString *objType = ValueToString(dic[@"objType"]);
                        NSString *objId = ValueToString(dic[@"objId"]);
                        if([objType isEqualToString:_model.model.objType] && [objId isEqualToString:_model.model.objId]) {
                            isReported = YES;
                            *stop = YES;
                        }
                    }];
                }
            }
            
            // 本地过滤黑名单用户的数据
            __block BOOL isBlacklist = NO;
            NSSet *blacklistSet = [SEGProfileManager seg_getBlacklist];
            if (blacklistSet.count > 0) {
                [blacklistSet enumerateObjectsUsingBlock:^(NSString *obj, BOOL * _Nonnull stop) {
                    if ([_model.user.userId isEqualToString:obj]) {
                        isBlacklist = YES;
                        *stop = YES;
                    }
                }];
            }

            if ((!isNotLike) && (!isReported) && (!isBlacklist)) {
                [tempNeighborhoodList addObject:_model];
            }
        }
        
        self.tips = [dicData valueForKey:@"tips"];
        
        // 组合新老数据
        [self.neighborhoodListArray appendObjects:tempNeighborhoodList];
        finished(YES, codeType, errorMessage);
    }];
}

/**
 查询单条邻里圈数据
 
 @param objId 数据ID
 @param objType 类型
 @param finished 回调
 */
- (void)queryNeighborhoodSingleWithObjId:(NSString *)objId
                                 objType:(NSString *)objType
                                finished:(SEGRequestCallBack)finished {
    NSDictionary *params = @{@"objId":objId,
                             @"objType":objType};
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/ugc-pgc/api/v4/queryNeighborhoodSingle",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        self.replaceModel = [SEGSharedNeihborhoodModel initWithDictionary:dicData];
        NSMutableArray *commentMArr = [NSMutableArray array];
        [dicData[@"comment"] enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull dic, NSUInteger idx, BOOL * _Nonnull stop) {
            SEGSharedCommentChildCommentInfoModel *infoModel = [SEGSharedCommentChildCommentInfoModel new];
            infoModel.content = dic[@"content"];
            SEGSharedCommonUserModel *userModel = [SEGSharedCommonUserModel new];
            userModel.nickName = dic[@"user"][@"nickName"];
            SEGSharedCommentChildCommentModel *commentModel = [SEGSharedCommentChildCommentModel new];
            commentModel.comment = infoModel;
            commentModel.user = userModel;
            [commentMArr addObject:commentModel];
        }];
        self.replaceModel.comment = commentMArr;
 
        finished(YES, codeType, errorMessage);
    }];
    
}

/**
 * 获取UGC详情 -转发
 * @author Wendy
 * @param objId  业务id
 * @param objType  类务类型
 */
- (void)requestForwardWithObjType:(NSString *)objType
                            objId:(NSString *)objId
                         finished:(SEGRequestCallBack)finished
{
    NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:objType, @"objType", objId, @"objId", nil];

    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kCMS_api,@"/bbs/forward.json",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* model = [dicData valueForKey:@"model"];
        if (!model || ![model isKindOfClass:[NSDictionary class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        self.fowardInfoModel = [SEGSharedFowardModel initWithDictionary:model];
        
        NSDictionary* user = [dicData valueForKey:@"user"];
        if (user && [user isKindOfClass:[NSDictionary class]]) {
            self.fowardUserModel = [SEGSharedCommonUserModel initWithDictionary:user];
        }
        
        finished(YES, codeType, errorMessage);
        
    }];
}

#pragma mark - 评论
///  新增问题答案
///
///  @param quizId         问题ID
///  @param answContent    答案内容
///  @param creator        回答问题者的NICK
///  @param byReviewId     如果回复的是主贴 byReviewId 和 byReviewNick 两个字段不传（消息接受者的用户ID，对于话题和PGC等系统发布的内容不传，其他情况要传）字段一定要传
///  @param byReviewNick   如果回复的是主贴 byReviewId 和 byReviewNick 两个字段不传（消息接受者的用户ID，对于话题和PGC等系统发布的内容不传，其他情况要传）字段一定要传
///  @param byReviewUserId 被回评者对象的用户ID，即记录创建者的用户Id
///  @param objSendUserId  对象发布者ID
///  @param objPicUrlForMsg  消息图片 主要是 2手 闲置 帮帮
///  @param objTitleForMsg  消息标题,如果是对PGC或者话题等系统发起的 objTitleForMsg为对应的标题，帮帮则为帮帮的标题（即问题的内容）否则objTitleForMsg为 评论内容
///  @param objeType
///  @param parentObjId    主题对象ID
///  @param operationType 邻里圈类型
///  @param sourceCommentId 对子评论进行回复时，传子评论的ID
///  @param isParent 是否是对主数据进行评论 1：是 2：不是
- (void)requestSaveQuizAnswerByquizId:(NSString*)quizId
                          answContent:(NSString*)answContent
                              creator:(NSString*)creator
                           byReviewId:(NSString*)byReviewId
                         byReviewNick:(NSString*)byReviewNick
                       byReviewUserId:(NSString*)byReviewUserId
                        objSendUserId:(NSString*)objSendUserId
                      objPicUrlForMsg:(NSString*)objPicUrlForMsg
                       objTitleForMsg:(NSString*)objTitleForMsg
                             objeType:(NSString*)objeType
                          parentObjId:(NSString*)parentObjId
                        parentObjType:(NSInteger)parentObjType
                        operationType:(NSString *)operationType
                      sourceCommentId:(NSString *)sourceCommentId
                             isParent:(NSString *)isParent
                             Finished:(SEGRequestCallBack)finished
{
    SEGProfileModel *account = [SEGProfileManager sharedManager].profile;
    NSString* communityName = [NSString stringWithFormat:@"来自%@ %@", account.city, account.communityName];
    NSString* communityId = account.jobCommunity;
    NSString* nickName = account.nickname;
    if ([nickName length] == 0) {
        nickName = account.nickname;
    }
    
    NSDictionary* tempDict = @{
                               @"quizId" : quizId,
                               @"answContent" : answContent,
                               @"creator" : nickName,
                               @"byReviewUserId" : byReviewUserId,
                               @"communityName" : communityName,
                               @"communityId" : communityId,
                               @"userId" : kAccountUserID,
                               @"objTitleForMsg" : objTitleForMsg,
                               @"objType" : objeType,//
                               @"parentObjId" : parentObjId,
                               @"parentObjType" : @(parentObjType),
                               @"isParent" : isParent
                               };
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:tempDict];
    if (byReviewNick) {
        [params setObject:byReviewNick forKey:@"byReviewNick"];
    }
    if (byReviewId) {
        [params setObject:byReviewId forKey:@"byReviewId"];
    }
    if (objPicUrlForMsg) {
        [params setObject:objPicUrlForMsg forKey:@"objPicUrlForMsg"];
    }
    if (operationType) {
        [params setObject:operationType forKey:@"operationType"];
    }
    if (sourceCommentId) {
        [params setObject:sourceCommentId forKey:@"sourceCommentId"];
    }
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kCMS_api,@"/quiz/saveQuizAnswer.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }

        finished(YES, codeType, errorMessage);
        
    }];
}

///  新增评论
///
///  @param objId          评论主题对象ID
///  @param objType        评论主题对象类型 1画报 2问题 3评论 4问题答案 9闲置2手 10 闲置出借
///  @param commentContent 评论内容
///  @param userId         评论者的用户ID
///  @param creator        评论者的创建者NICK
///  @param byReviewId     被回评者对象IDbyReviewId、byReviewNick：回复的是主贴，两个字段不传
///  @param byReviewNick   被回评者对象的昵称byReviewId、byReviewNick：回复的是主贴，两个字段不传
///  @param byReviewUserId 被回评者对象的用户ID，即记录创建者的用户Id byReviewUserId：回复的是主贴，回复评论的评论，字段一定要传，内容：［话题、PGC］不传内容，其他的都传
///  @param communityName  城市+小区名称
///  @param communityId    小区ID
///  @param objPicUrlForMsg  消息图片 主要是 2手 闲置 帮帮
///  @param objTitleForMsg  消息标题,如果是对PGC或者话题等系统发起的 objTitleForMsg为对应的标题，帮帮则为帮帮的标题（即问题的内容）否则objTitleForMsg为 评论内容
///  @param parentObjType   枚举值 SEGUserType
///  @param operationType 邻里圈类型
///  @param isParent 是否是对主数据进行评论 1：是 2：不是
- (void)requestCommentSaveByobjId:(NSString*)objId
                          objType:(NSString*)objType
                   commentContent:(NSString*)commentContent
                          creator:(NSString*)creator
                       byReviewId:(NSString*)byReviewId
                     byReviewNick:(NSString*)byReviewNick
                   byReviewUserId:(NSString*)byReviewUserId
                  objPicUrlForMsg:(NSString*)objPicUrlForMsg
                   objTitleForMsg:(NSString*)objTitleForMsg
                      parentObjId:(NSString*)parentObjId
                    parentObjType:(NSInteger)parentObjType
                    operationType:(NSString *)operationType
                  sourceCommentId:(NSString *)sourceCommentId
                         isParent:(NSString *)isParent
                         Finished:(SEGRequestCallBack)finished
{
    SEGProfileModel *account = [SEGProfileManager sharedManager].profile;
    NSString* communityName = [NSString stringWithFormat:@"来自%@ %@", account.city, account.communityName];
    NSString* communityId = account.jobCommunity;
    NSString* nickName = account.nickname;
    if ([nickName length] == 0) {
        nickName = account.tel;
    }
    
    NSDictionary* tempDict = @{
                               @"objId" : objId,
                               @"objType" : objType,
                               @"commentContent" : commentContent,
                               @"userId" : kAccountUserID,
                               @"creator" : creator,
                               @"byReviewUserId" : byReviewUserId,
                               @"communityName" : communityName,
                               @"communityId" : communityId,
                               @"objTitleForMsg" : objTitleForMsg,
                               @"parentObjId" : parentObjId,
                               @"parentObjType" : @(parentObjType),
                               @"isParent" : isParent
                               };
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:tempDict];
    if (byReviewNick) {
        [params setObject:byReviewNick forKey:@"byReviewNick"];
    }
    if (byReviewId) {
        [params setObject:byReviewId forKey:@"byReviewId"];
    }
    if (objPicUrlForMsg) {
        [params setObject:objPicUrlForMsg forKey:@"objPicUrlForMsg"];
    }
    if (operationType) {
        [params setObject:operationType forKey:@"operationType"];
    }
    if (sourceCommentId) {
        [params setObject:sourceCommentId forKey:@"sourceCommentId"];
    }
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kCMS_api,@"/comment/save.json",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        finished(YES, codeType, errorMessage);
        
    }];
}

#pragma mark - 举报
/**
 * 请求举报原因
 * @author Wendy
 * @param finished 回调结果
 */
- (void)requestReportReasonType:(SEGNetworkReponse)finished
{
    NSDictionary *params = @{@"type":@"reportReasonType"};
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/dictionaryList",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:GET
                                        URLString:urlString
                                       parameters:params
                                         finished:^(BOOL isSuccessed,
                                                    id result,
                                                    NSString *codeType,
                                                    NSString *errorMessage) {
                                             if (!isSuccessed) {
                                                 finished(NO, nil, codeType, errorMessage);
                                                 return;
                                             }
                                             
                                             NSDictionary* dicData = [result valueForKey:@"data"];
                                             if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
                                                 finished(NO, nil, codeType, errorMessage);
                                                 return;
                                             }
                                             
                                             NSArray* list = [dicData valueForKey:@"resultList"];
                                             
                                             if (!list || ![list isKindOfClass:[NSArray class]]) {
                                                 finished(NO, nil, codeType, errorMessage);
                                                 return;
                                             }
                                             
                                             NSMutableArray* arrM = [NSMutableArray array];
                                             [list enumerateObjectsUsingBlock:^(NSDictionary* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                                 SEGReportReasonModel *_model = [SEGReportReasonModel initWithDictionary:obj];
                                                 [arrM addObject:_model];
                                             }];

                                             self.reportReasonArray = [arrM copy];
                                             finished(isSuccessed,  result, codeType, errorMessage);
                                         }];
}

/**
 * 提交举报
 * @author Wendy
 * @param byReportedId 被举报人ID
 * @param byReportedNickname  被举报人昵称
 * @param byReportedContId  被举报内容ID
 * @param byReportedContTypeCode  被举报内容类型编码
 * @param byReportedContDetails  被举报内容详情文字
 * @param byReportedContImages  被举报内容详情图片（多个，号分隔）
 * @param reportReasonCode  举报理由编码
 * @param reportReasonName  举报理由名称
 * @param pgcTitle pgc标题
 * @param finished 回调结果
 */
- (void)requestSubmitComplaintWithByReportedId:(NSString *)byReportedId
                            byReportedNickname:(NSString *)byReportedNickname
                              byReportedContId:(NSString *)byReportedContId
                        byReportedContTypeCode:(NSString *)byReportedContTypeCode
                         byReportedContDetails:(NSString *)byReportedContDetails
                          byReportedContImages:(NSString *)byReportedContImages
                              reportReasonCode:(NSString *)reportReasonCode
                              reportReasonName:(NSString *)reportReasonName
                                      pgcTitle:(NSString *)pgcTitle
                                      finished:(SEGRequestCallBack)finished
{
    NSDictionary *temp = @{
                             @"informantId" : kAccountUserID,
                             @"informantNickname" : kAccountNickName,
                             @"byReportedId" : byReportedId ?: @"",
                             @"byReportedNickname" : byReportedNickname ?: @"",
                             @"byReportedContId" : byReportedContId ?: @"",
                             @"byReportedContTypeCode" : byReportedContTypeCode ?: @"",
                             @"byReportedContDetails" : byReportedContDetails ?: @"",
                             @"byReportedContImages" : byReportedContImages ?: @"",
                             @"reportReasonCode" : reportReasonCode ?: @"",
                             @"reportReasonName" : reportReasonName ?: @""
                             };
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:temp];
    if (pgcTitle && pgcTitle.length > 0) {
        [params setObject:pgcTitle forKey:@"pgcTitle"];
    }
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/report/reportAdd",nil];

    [SEGHTTPBusinessManager seg_requestJSONWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }

        finished(YES, codeType, errorMessage);
        
    }];
}

/**
 * 请求求助标签
 * @author Wendy
 * @param finished 回调结果
 */
- (void)requestHelpLabelList:(SEGNetworkReponse)finished
{
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/api/v4/getLabelList",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:GET
                                        URLString:urlString
                                       parameters:nil
                                         finished:^(BOOL isSuccessed,
                                                    id result,
                                                    NSString *codeType,
                                                    NSString *errorMessage) {
                                             if (!isSuccessed) {
                                                 finished(NO, nil, codeType, errorMessage);
                                                 return;
                                             }
                                             
                                             NSDictionary* dicData = [result valueForKey:@"data"];
                                             if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
                                                 finished(NO, nil, codeType, errorMessage);
                                                 return;
                                             }
                                             
                                             NSArray* list = [dicData valueForKey:@"askForHelp"];
                                             if (list && [list isKindOfClass:[NSArray class]]) {
                                                 NSMutableArray* arrM = [NSMutableArray array];
                                                 [list enumerateObjectsUsingBlock:^(NSDictionary* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                                     SEGHelpLabelModel *model = [SEGHelpLabelModel yy_modelWithDictionary:obj];
                                                     [arrM addObject:model];
                                                 }];
                                                 self.askForHelpArray = [arrM copy];
                                             }
                                             
                                             NSArray* provideHelpList = [dicData valueForKey:@"provideHelp"];
                                             if (provideHelpList && [provideHelpList isKindOfClass:[NSArray class]]) {
                                                 NSMutableArray* arrM = [NSMutableArray array];
                                                 [provideHelpList enumerateObjectsUsingBlock:^(NSDictionary* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                                     SEGHelpLabelModel *model = [SEGHelpLabelModel yy_modelWithDictionary:obj];
                                                     [arrM addObject:model];
                                                 }];
                                                 self.provideHelpArray = [arrM copy];
                                             }
                                             
                                             finished(isSuccessed,  result, codeType, errorMessage);
                                         }];
}

#pragma mark - 每日精选
/**
 * 获取每日精选主题
 * @author Wendy
 * @param pageNo  页码
 * @param pageLimit  每页数据条数
 */
- (void)requestDailySubjectArrayWithPageNo:(NSString *)pageNo
                                 pageLimit:(NSString *)pageLimit
                                  finished:(SEGRequestCallBack)finished
{
    NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:pageNo, @"pageNo", pageLimit, @"pageLimit", nil];
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/topic/queryTopicAppPage",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary *paginator = [dicData valueForKey:@"paginator"];
        if (paginator && [paginator isKindOfClass:[NSDictionary class]]) {
            NSString *total = ValueToString([paginator valueForKey:@"totalPages"]);
            if (total && total.length > 0) {
                self.dailySubjectTotalPages = [total integerValue];
            }else {
                self.dailySubjectTotalPages = 1;
            }
        }
        
        NSArray* list = [dicData valueForKey:@"topicList"];
        
        if (!list || ![list isKindOfClass:[NSArray class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        if ([pageNo integerValue] != 1 && [list isKindOfClass:[NSArray class]] && list.count == 0) {
            finished(NO, codeType, @"没有更多的数据！");
            return;
        }
        
        if ([pageNo integerValue] == 1) { //顶部刷新
            self.recommendSubjectArray = @[];
        }
        
        NSMutableArray* arrM = [NSMutableArray arrayWithCapacity:list.count];
        for (NSDictionary* dic in list) {
            SEGDailySubjectModel* _model = [SEGDailySubjectModel initWithDictionary:dic];
            [arrM addObject:_model];
        }
        NSMutableArray* _tmpData = [NSMutableArray arrayWithArray:self.recommendSubjectArray];
        [_tmpData addObjectsFromArray:arrM];
        
        self.recommendSubjectArray = _tmpData;
        
        finished(YES, codeType, errorMessage);
        
    }];
}

/**
 * 获取侧滑推荐位--查看更多
 * @author Wendy
 * @param pageNo  页码
 * @param pageLimit  每页数据条数
 * @param topicId 所属主题的ID
 */
- (void)requestDailyContentArrayWithPageNo:(NSString *)pageNo
                                 pageLimit:(NSString *)pageLimit
                                   topicId:(NSString *)topicId
                                  finished:(SEGRequestCallBack)finished
{
    NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:pageNo, @"pageNo", pageLimit, @"pageLimit", topicId, @"topicId", nil];
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/topic/queryRecommendPage",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary *paginator = [dicData valueForKey:@"paginator"];
        if (paginator && [paginator isKindOfClass:[NSDictionary class]]) {
            NSString *total = ValueToString([paginator valueForKey:@"totalPages"]);
            if (total && total.length > 0) {
                self.dailyContentTotalPages = [total integerValue];
            }else {
                self.dailyContentTotalPages = 1;
            }
            
            
            NSString *totalCount = ValueToString([paginator valueForKey:@"totalCount"]);
            if (totalCount && totalCount.length > 0) {
                self.dailyContentTotalCount = [totalCount integerValue];
            }else {
                self.dailyContentTotalCount = 1;
            }
        }
        
        NSArray* list = [dicData valueForKey:@"relevanceList"];
        
        if (!list || ![list isKindOfClass:[NSArray class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        if ([pageNo integerValue] != 1 && [list isKindOfClass:[NSArray class]] && list.count == 0) {
            finished(NO, codeType, @"没有更多的数据！");
            return;
        }
        
        if ([pageNo integerValue] == 1) { //顶部刷新
            self.recommendContentArray = @[];
        }
        
        NSMutableArray* arrM = [NSMutableArray arrayWithCapacity:list.count];
        for (NSDictionary* dic in list) {
            SEGDailyContentModel* _model = [SEGDailyContentModel initWithDictionary:dic];
            [arrM addObject:_model];
        }
        NSMutableArray* _tmpData = [NSMutableArray arrayWithArray:self.recommendContentArray];
        [_tmpData addObjectsFromArray:arrM];
        
        self.recommendContentArray = _tmpData;
        
        finished(YES, codeType, errorMessage);
        
    }];
}

#pragma mark - getter
- (NSArray<SEGSharedTopLineModel *> *)topLineArray
{
    if (!_topLineArray) {
        _topLineArray = [NSArray array];
    }
    return _topLineArray;
}

- (NSArray<SEGSharedRecommendModel *> *)recommendArray
{
    if (!_recommendArray) {
        _recommendArray = [NSArray array];
    }
    return _recommendArray;
}

- (NSArray<SEGSharedRecommendModel *> *)recommendMoreArray
{
    if (!_recommendMoreArray) {
        _recommendMoreArray = [NSArray array];
    }
    return _recommendMoreArray;
}

- (NSArray<SEGReportReasonModel *> *)reportReasonArray
{
    if (!_reportReasonArray) {
        _reportReasonArray = [NSArray array];
    }
    return _reportReasonArray;
}

- (NSArray<SEGHelpLabelModel *> *)askForHelpArray
{
    if (!_askForHelpArray) {
        _askForHelpArray = [NSArray array];
    }
    return _askForHelpArray;
}

- (NSArray<SEGHelpLabelModel *> *)provideHelpArray
{
    if (!_provideHelpArray) {
        _provideHelpArray = [NSArray array];
    }
    return _provideHelpArray;
}
- (NSArray<SEGDailySubjectModel *> *)recommendSubjectArray
{
    if (!_recommendSubjectArray) {
        _recommendSubjectArray = [NSArray array];
    }
    return _recommendSubjectArray;
}

- (NSArray<SEGDailyContentModel *> *)recommendContentArray
{
    if (!_recommendContentArray) {
        _recommendContentArray = [NSArray array];
    }
    return _recommendContentArray;
}

@end
